//
//  CollectTableViewCell.m
//  MoGuRadio
//
//  Created by dlios on 15/7/3.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "CollectTableViewCell.h"
#import "AllPageCollectionViewCell.h"
#import "AllPageCollectionReusableView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "MyRadioViewController.h"

#define COLLECT_REUSE @"MainCollectionViewReuseIdentifier"

@interface CollectTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *mainCollectionView;
@property (nonatomic, retain) NSMutableArray *remmendArr;

@end


@implementation CollectTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self dataHandlerHotFoucs];
    
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //规定每个cell大小
        flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 7 / 25, [UIScreen mainScreen].bounds.size.height * 13 / 85);
        //一个section距离边缘上左下右的距离
        flowLayout.sectionInset = UIEdgeInsetsMake([UIScreen mainScreen].bounds.size.width / 25, [UIScreen mainScreen].bounds.size.width / 25, [UIScreen mainScreen].bounds.size.width / 25, [UIScreen mainScreen].bounds.size.width / 25);
        
        //在一行里面.item之间的最小间距;
        flowLayout.minimumLineSpacing = [UIScreen mainScreen].bounds.size.height / 13;
        //行之间的间距*(行;是垂直于滚动方向的叫行)
        //flowLayout.minimumInteritemSpacing = 20;
        
        self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 5) collectionViewLayout:flowLayout];
        
        
        self.mainCollectionView.backgroundColor = [UIColor colorWithRed:0.88 green:0.90 blue:0.738 alpha:0.2];
        self.mainCollectionView.delegate = self;
        self.mainCollectionView.dataSource = self;
//        self.mainCollectionView.scrollEnabled = NO;
        [self.contentView addSubview:self.mainCollectionView];
        
        
        [self.mainCollectionView registerClass:[AllPageCollectionViewCell class] forCellWithReuseIdentifier:COLLECT_REUSE];
        
        [self.mainCollectionView registerClass:[AllPageCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
        [_mainCollectionView release];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.mainCollectionView.frame = CGRectMake(0, 0, LBVIEW_WIDTH1, LBVIEW_HEIGHT1 / 2);
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath Arr:self.remmendArr];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.remmendArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:COLLECT_REUSE forIndexPath:indexPath];
    
    // 数组保护
    if (self.remmendArr.count > indexPath.item) {
        
        cell.myLabel.text = [[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"rname"];
        NSString *imageStr = [[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"pic"];
        
        NSURL *url = [NSURL URLWithString:imageStr];
        
        [cell.myImageView setImageWithURL:url];
    }
    
    return cell;
}


#warning mark UICollectionView的headerSection的协议方法
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    
//    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        
//        AllPageCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        
//        reuseView.myLabel.text = @"热点推荐";
//        
//        return reuseView;
//    }
//    
//    return nil;
//    
//}


- (void)dataHandlerHotFoucs
{
    
    NSString *dataStr = @"http://api.kaolafm.com/api/v3.5/resource/list?ids=16_-17_-18_-14_5-19_-20_-15_5-21_&installid=0000TLD3&udid=eca85b7ac579ac2b4a4f9fec6744f8fd&sessionid=eca85b7ac579ac2b4a4f9fec6744f8fd1433842263948&imsi=460036090834220&operator=3&network=1&timestamp=1433842412&playid=0341feb3d321649db62216f79fcfa2f5&sign=f3e82c19c896a40926481f50f62d2a8c&appid=0&channel=A-baidu&version=3.6.1&devicetype=0&resolution=480*854&";
    
    dataStr = [dataStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:dataStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [responseObject objectForKey:@"result"];
        
        self.remmendArr = [[[dic objectForKey:@"dataList"] objectAtIndex:2] objectForKey:@"dataList"];
        
       
        [self.mainCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误信息是==%@", error);
        
    }];
    
}


- (void)dealloc
{
    [_remmendArr release];
    [super dealloc];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
